﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class MenuScreen : ComponentView
    {
        Game1 _game;
        bool isLeftClick = false;

        enum CHOOSE_OPTION
        {
            Not_Choose,
            Play,
            Option,
            Exit,
            Yes,
            No
        };
        CHOOSE_OPTION chooseOption = CHOOSE_OPTION.Not_Choose;

        Button _play;
        Button _exit;
        Button _option;
        ImageView _logo;
        ImageView _backgroundMenu;
        Label _text;

        ComponentView _exitPanel;
        Button _yes;
        Button _no;

        public MenuScreen(Game1 game)
        {
            Size = new Vector2(GameBase.Constant.screenWidth, GameBase.Constant.screenHeight);
            _game = game;

            // load action in menu screen
            List<ActionHelper> listAction = ActionXMLReader.loadAllAction(@"View/GameScreen/ActionMenu/ActionButtonAtMenuScreen1.xml");

            // TODO: load another action menu screen in file xml
            //List<ActionHelper> listAction = ActionXMLReader.loadAllAction(@"View/GameScreen/ActionMenu/ActionButtonAtMenuScreen2.xml");

            _backgroundMenu = new ImageView(@"Resources/background6", new Vector2(0, 0), 1);
            _play = new Button(@"Resources/play", ((MoveAction)listAction[0]).BeginPosition, 1);
            _option = new Button(@"Resources/option", ((MoveAction)listAction[1]).BeginPosition, 1);
            _exit = new Button(@"Resources/exit", ((MoveAction)listAction[2]).BeginPosition, 1);
            _logo = new ImageView(@"Resources/logo", Vector2.Zero, 1);
            _logo.Position = new Vector2((GameBase.Constant.screenWidth - _logo.Size.X) / 2, 100);

            _play.Action = listAction[0];
            _option.Action = listAction[1];
            _exit.Action = listAction[2];
            _logo.Action = listAction[3];

            listAction[0].View = _play;
            listAction[1].View = _option;
            listAction[2].View = _exit;
            listAction[3].View = _logo;

            _play.Position = ((MoveAction)listAction[0]).BeginPosition;
            _option.Position = ((MoveAction)listAction[1]).BeginPosition;
            _exit.Position = ((MoveAction)listAction[2]).BeginPosition;

            _option.subscribe(_play.Action);
            _exit.subscribe(_option.Action);
            _play.subscribe(_exit.Action);
            this.subscribe(_exit.Action);

            _play.Action.start();
            _logo.Action.start();

            _exitPanel = new ComponentView(@"Resources/panel", new Vector2(1400, 250), new Vector2(400, 250));
            _exitPanel.Alpha = 0.4f;
            _exitPanel.Action = new MoveAction(_exitPanel, new Vector2(750, 250), 10f, 4f);

            _yes = new Button(@"Resources/yes", new Vector2(_exitPanel.Size.X / 2 - 105, _exitPanel.Size.Y - 70), 0.5f);
            _no = new Button(@"Resources/no", new Vector2(_exitPanel.Size.X / 2 - 105, _exitPanel.Size.Y - 140), 0.5f);
            _text = new Label(@"Resources/fontNormal", @"Do you want to exit game ?", new Vector2(20, 30), Color.Blue, 0.7f);

            _exitPanel.addToSubView(_yes);
            _exitPanel.addToSubView(_no);
            _exitPanel.addToSubView(_text);

            this.addToSubView(_backgroundMenu);
            this.addToSubView(_logo);
            this.addToSubView(_play);
            this.addToSubView(_option);
            this.addToSubView(_exit);
            this.addToSubView(_exitPanel);
        }

        bool _isFirstTimeClickNo = true;
        bool _isExitPanelAppear = false;

        public override void update(GameTime gameTime)
        {
            #region Mouse processing
            MouseState ms = Mouse.GetState();
            Vector2 targetPos = new Vector2(ms.X, ms.Y);

            if (ms.LeftButton == ButtonState.Pressed)
                isLeftClick = true;
            if (ms.LeftButton == ButtonState.Released && isLeftClick)
            {
                isLeftClick = false;
                if (_play.isContainPoint(targetPos))
                {
                    chooseOption = CHOOSE_OPTION.Play;
                    reverseAllAction();
                }
                else if (_exit.isContainPoint(targetPos))
                {
                    if (!_isFirstTimeClickNo)
                    {
                        _exitPanel.Action.reverse();
                    }
                    _isFirstTimeClickNo = false;
                    _exitPanel.Action.start();
                    _isExitPanelAppear = !_isExitPanelAppear;
                }
                else if (_option.isContainPoint(targetPos))
                {
                    chooseOption = CHOOSE_OPTION.Option;
                    reverseAllAction();
                }
                else if (_no.isContainPoint(targetPos))
                {
                    _exitPanel.Action.reverse();
                    _exitPanel.Action.start();
                    _isExitPanelAppear = false;
                }
                else if (_yes.isContainPoint(targetPos))
                {
                    chooseOption = CHOOSE_OPTION.Exit;
                    reverseAllAction();
                }
            }
            #endregion

            base.update(gameTime);

            _play.update(gameTime);
            _option.update(gameTime);
            _exit.update(gameTime);
            _exitPanel.update(gameTime);
        }

        private void reverseAllAction()
        {
            _play.Action.reverse();
            _option.Action.reverse();
            _exit.Action.reverse();
            _play.Action.start();

            _logo.Action.reverse();
            _logo.Action.start();

            if (_isExitPanelAppear)
            {
                _isExitPanelAppear = false;
                _exitPanel.Action.reverse();
                _exitPanel.Action.start();
            }
        }

        public void playAnimation()
        {
            reverseAllAction();
        }

        #region ISubscriber
        bool _isFirstTimeOptionScreen = true;
        bool _isFirstTimePlaySettingScreen = true;

        public override bool notify(string message)
        {
            base.notify(message);

            switch (chooseOption)
            {
                case CHOOSE_OPTION.Play:
                    Game1.currentGameScreen = Game1.GameScreen.PlaySetting;

                    if (!_isFirstTimePlaySettingScreen)
                    {
                        _game.PlaySettingScreen.playAnimation();
                    }
                    else
                        _isFirstTimePlaySettingScreen = false;

                    break;
                case CHOOSE_OPTION.Option:
                    Game1.currentGameScreen = Game1.GameScreen.Option;
                    
                    if (!_isFirstTimeOptionScreen)
                    {
                        _game.OptionScreen.playAnimation();
                    }
                    else
                        _isFirstTimeOptionScreen = false;

                    break;
                case CHOOSE_OPTION.Exit:
                    Game1.currentGameScreen = Game1.GameScreen.Exit;
                    break;
            }
            chooseOption = CHOOSE_OPTION.Not_Choose;
            return true;
        }
        #endregion
    }
}
